package com.yanqu.road.server.manager.cookboy;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.cookboy.CookBoyMatch;
import com.yanqu.road.entity.activity.cookboy.CookBoyMatchDish;
import com.yanqu.road.entity.activity.cookboy.CookBoyMatchReward;
import com.yanqu.road.entity.activity.cookboy.process3.CookBoy3BanquetReward;
import com.yanqu.road.entity.enums.activity.eActivityCookBoyType;
import com.yanqu.road.entity.enums.eActivityType;
import com.yanqu.road.pb.cookboy.CookBoyProto;
import com.yanqu.road.pb.player.PlayerProto;
import com.yanqu.road.server.TempCrossMgr;
import com.yanqu.road.server.manager.activity.ActivityMgr;
import com.yanqu.road.server.manager.activity.CrossRelationActivityMgr;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.utils.date.DateHelper;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.mina.util.ConcurrentHashSet;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class CrossCookBoyMgr extends TempCrossMgr {

    private static Logger logger = LogManager.getLogger(CrossCookBoyMgr.class.getName());

    private static Map<Integer, CrossCookBoy> crossCookBoyMap = new ConcurrentHashMap<>();//活动map

    private static Map<Integer, CrossCookBoy2> crossCookBoy2Map = new ConcurrentHashMap<>();//小当家2活动map

    private static Map<Integer, CrossCookBoy3> crossCookBoy3Map = new ConcurrentHashMap<>();//小当家3活动map

    private static Map<Integer, Object> activityLock;


    //阶段1晋级阶段2(支持不同跨服导活动)
    public static int windUpCookBoy1User(int activityId, Map<Integer, Long> userMap) {
        ActivityInfo ck1Ac = CrossCookBoyActivityMgr.getOpenActivityInfo(activityId);
        List<ActivityInfo> activityInfoList = CrossCookBoyActivityMgr.getOpenActivityInfoList(eActivityType.CookingBoyActivity.getValue(), eActivityCookBoyType.Process2.getChildType());
        for (ActivityInfo activityInfo : activityInfoList) {
            //阶段2活动和阶段1活动有重叠或者只有一个活动
            if (activityInfoList.size() == 1 || CrossCookBoyActivityMgr.isActivityContain(ck1Ac, activityInfo)) {
                synchronized (getActivityLock(activityId)) {
                    CrossCookBoy2 crossCookBoy2 = getCrossCookBoy2(activityInfo.getActivityId());
                    if (crossCookBoy2 != null) {
                        crossCookBoy2.addPromotionUser(userMap);
                        return activityInfo.getActivityId();
                    }
                }
            }
        }
        return 0;
    }

    //阶段2晋级阶段3
    public static int windUpCookBoy2User(int activityId, Map<Integer, Long> userMap) {
        ActivityInfo ck1Ac = CrossCookBoyActivityMgr.getOpenActivityInfo(activityId);
        List<ActivityInfo> activityInfoList = CrossCookBoyActivityMgr.getOpenActivityInfoList(eActivityType.CookingBoyActivity.getValue(), eActivityCookBoyType.Process3.getChildType());
        for (ActivityInfo activityInfo : activityInfoList) {
            //阶段3活动和阶段1活动有重叠或者只有一个活动
            if (activityInfoList.size() == 1 || CrossCookBoyActivityMgr.isActivityContain(ck1Ac, activityInfo)) {
                synchronized (getActivityLock(activityId)) {
                    CrossCookBoy3 crossCookBo3 = getCrossCookBoy3(activityInfo.getActivityId());
                    if (crossCookBo3 != null) {
                        crossCookBo3.addPromotionUser(userMap);
                        return activityInfo.getActivityId();
                    }
                }
            }
        }
        return 0;
    }
    /*↑↑↑↑晋级↑↑↑↑↑↑*/

    public static String getCookBoy3BanquetTimes(int activityId, String param) {
            CrossCookBoy3 crossCookBoy3 = getCrossCookBoy3(activityId);
            if (crossCookBoy3 != null) {
                return crossCookBoy3.getCookBoy3BanquetTimes(param);
            }
        return "";
    }

    public static String getCookBoy3BanquetUserCount(int activityId, String param) {
            CrossCookBoy3 crossCookBoy3 = getCrossCookBoy3(activityId);
            if (crossCookBoy3 != null) {
                return crossCookBoy3.getCookBoy3BanquetUserCount(param);
            }
        return "";
    }

    public static void getBanquetJoiner(int activityId, long serverId, long userId, long homeUserId, int banquetId, int page, int num) {
        CrossCookBoy3 crossCookBoy3 = getCrossCookBoy3(activityId);
        if (crossCookBoy3 != null) {
            crossCookBoy3.getBanquetJoiner(serverId, userId, homeUserId, banquetId, page, num);
        }
    }

    public static void getUserBanquet(int activityId, long serverId, long userId, long homeUserId) {
            CrossCookBoy3 crossCookBoy3 = getCrossCookBoy3(activityId);
            if (crossCookBoy3 != null) {
                crossCookBoy3.getUserBanquet(serverId, userId, homeUserId);
            }
    }

    public static void addPromotion3User(int activityId, long serverId, List<CookBoyProto.CrossCookBoyWindUpCookerTempMsg> userListMsg) {
        synchronized (getActivityLock(activityId)) {
            CrossCookBoy3 crossCookBoy3 = getCrossCookBoy3(activityId);
            if (crossCookBoy3 != null) {
                crossCookBoy3.addPromotionUser(serverId, userListMsg);
            }
        }
    }

    private static CrossCookBoy3 getCrossCookBoy3(int activityId) {
        if (crossCookBoy3Map.containsKey(activityId)) {
            return crossCookBoy3Map.get(activityId);
        } else {
            CrossCookBoyActivityMgr.reloadActivityInfo(activityId);
            ActivityInfo acInfo = CrossCookBoyActivityMgr.getOpenActivityInfo(activityId);
            if (acInfo != null) {
                if(acInfo.getType() != eActivityType.CookingBoyActivity.getValue()){
                    return null;
                }
                CrossCookBoy3 crossCookBoy3 = new CrossCookBoy3(acInfo);
                if (crossCookBoy3.init()) {
                    crossCookBoy3Map.put(activityId, crossCookBoy3);
                } else {
                    logger.error("跨服小当家3活动" + activityId + "加载失败");
                }
                return crossCookBoy3Map.get(activityId);
            }
            return null;
        }
    }

    public static void syncCooker3Table(int activityId, long serverId, long userId, long homeUserId) {
            CrossCookBoy3 crossCookBoy3 = getCrossCookBoy3(activityId);
            if (crossCookBoy3 != null) {
                crossCookBoy3.syncCooker3Table(serverId, userId, homeUserId);
            }
    }

    public static void openBanquet(int activityId, long serverId, long userId, int itemId) {
        synchronized (getActivityLock(activityId)) {
            CrossCookBoy3 crossCookBoy3 = getCrossCookBoy3(activityId);
            if (crossCookBoy3 != null) {
                crossCookBoy3.openBanquet(serverId, userId, itemId);
            }
        }
    }

    public static int joinBanquet(int activityId, long serverId, long joinUserId, long homeUserId, int banquetId) {
        CrossCookBoy3 crossCookBoy3 = getCrossCookBoy3(activityId);
        if (crossCookBoy3 != null) {
            return crossCookBoy3.joinBanquet(serverId, joinUserId, homeUserId, banquetId);
        }
        return GameErrorCode.E_ACTIVITY_NO_FOUND;
    }

    public static CookBoy3BanquetReward getBanquetReward(int activityId, long userId, int no) {
        CookBoy3BanquetReward cookBoyBanquetReward = new CookBoy3BanquetReward();
            CrossCookBoy3 crossCookBo3y = getCrossCookBoy3(activityId);
            if (crossCookBo3y != null) {
                return crossCookBo3y.getBanquetReward(userId, no);
            }
        cookBoyBanquetReward.setRet(GameErrorCode.E_RELATION_ACTIVITY_NO_IN_TIME);
        return cookBoyBanquetReward;
    }

    /*↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓小当家二↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓1*/
    public static int joinTable(int activityId, long serverId, long joinUserId, long homeId, int tableId, int spoonId
            , int trueActivityId, PlayerProto.PlayerBaseTempMsg joinUserInfo) {
            CrossCookBoy2 crossCookBoy2 = getCrossCookBoy2(activityId);
            if (crossCookBoy2 != null) {
                return crossCookBoy2.joinTable(serverId, joinUserId, homeId, tableId, spoonId, trueActivityId, joinUserInfo);
            }
        return GameErrorCode.E_ACTIVITY_NO_FOUND;
    }

    public static List<Integer> getUserForbiddenSpoonIdList(int activityId, long userId) {
        CrossCookBoy2 crossCookBoy2 = getCrossCookBoy2(activityId);
        if (crossCookBoy2 != null) {
            return crossCookBoy2.getUserForbiddenSpoonIdList(userId);
        }
        return new ArrayList<>();
    }

    public static void serveDish(int activityId, long serverId, long userId, Map<Integer, Integer> dishMap) {
        synchronized (getActivityLock(activityId)) {
            CrossCookBoy2 crossCookBoy2 = getCrossCookBoy2(activityId);
            if (crossCookBoy2 != null) {
                crossCookBoy2.serveDish(serverId, userId, dishMap);
            }
        }
    }

    public static void syncCookerHomeTable(int activityId, long serverId, long userId, long homeUser, int page, int num) {
            CrossCookBoy2 crossCookBoy2 = getCrossCookBoy2(activityId);
            if (crossCookBoy2 != null) {
                crossCookBoy2.syncCookerHomeTable(serverId, userId, homeUser, page, num);
            }
    }

    public static void getCookerTable(int activityId, long serverId, long userId, long homeUser, int tableId) {
            CrossCookBoy2 crossCookBoy2 = getCrossCookBoy2(activityId);
            if (crossCookBoy2 != null) {
                crossCookBoy2.getCookerTable(serverId, userId, homeUser, tableId);
            }
    }

    public static void syncCookerTable(int activityId, long serverId, long userId) {
            CrossCookBoy2 crossCookBoy2 = getCrossCookBoy2(activityId);
            if (crossCookBoy2 != null) {
                crossCookBoy2.syncCookerTable(serverId, userId);
            }
    }

    public static void getRecord(int activityId, long serverId, long userId) {
            CrossCookBoy2 crossCookBoy2 = getCrossCookBoy2(activityId);
            if (crossCookBoy2 != null) {
                crossCookBoy2.getRecord(serverId, userId);
            }
    }

    public static void getUnionRelation(int activityId, long serverId, long userId, List<Long> unionList, List<Long> relationList) {
            CrossCookBoy2 crossCookBoy2 = getCrossCookBoy2(activityId);
            if (crossCookBoy2 != null) {
                crossCookBoy2.getUnionRelation(serverId, userId, unionList, relationList);
            }
    }

    public static void searchUser(int activityId, long serverId, long userId, String searchUserId) {
            CrossCookBoy2 crossCookBoy2 = getCrossCookBoy2(activityId);
            if (crossCookBoy2 != null) {
                crossCookBoy2.searchUser(serverId, userId, searchUserId);
            }
    }

    public static void addPromotion2User(int activityId, long serverId, List<CookBoyProto.CrossCookBoyWindUpCookerTempMsg> userListMsg) {
        synchronized (getActivityLock(activityId)) {
            CrossCookBoy2 crossCookBoy2 = getCrossCookBoy2(activityId);
            if (crossCookBoy2 != null) {
                crossCookBoy2.addPromotionUser(serverId, userListMsg);
            }
        }
    }

    //获取随机可以加入宴会的用户
    public static void getRandomCanJoinUser(int activityId, long serverId, long userId, long nowUserId,List<Integer> spoonIdList) {
            CrossCookBoy2 crossCookBoy2 = getCrossCookBoy2(activityId);
            if (crossCookBoy2 != null) {
                crossCookBoy2.getRandomCanJoinUser(serverId, userId, nowUserId,spoonIdList);
            }
    }

    //禁止汤勺进食
    public static void forbidSpoon(int activityId, long serverId, long userId, List<Integer> forbidSpoonIdList) {
        CrossCookBoy2 crossCookBoy2 = getCrossCookBoy2(activityId);
        if (crossCookBoy2 != null) {
            crossCookBoy2.forbidSpoon(serverId, userId, forbidSpoonIdList);
        }
    }

    private static CrossCookBoy2 getCrossCookBoy2(int activityId) {
        if (crossCookBoy2Map.containsKey(activityId)) {
            return crossCookBoy2Map.get(activityId);
        } else {
            CrossCookBoyActivityMgr.reloadActivityInfo(activityId);
            ActivityInfo acInfo = CrossCookBoyActivityMgr.getOpenActivityInfo(activityId);
            if (acInfo != null) {
                if(acInfo.getType() != eActivityType.CookingBoyActivity.getValue()){
                    return null;
                }
                CrossCookBoy2 crossCookBoy2 = new CrossCookBoy2(acInfo);
                if (crossCookBoy2.init()) {
                    crossCookBoy2Map.put(activityId, crossCookBoy2);
                } else {
                    logger.error("跨服小当家2活动" + activityId + "加载失败");
                }
                return crossCookBoy2Map.get(activityId);
            }
        }
        return null;
    }

    public static void addCookBoy2Horse(int activityId, long serverId, long userId, int dishId, long time) {
            CrossCookBoy2 crossCookBoy2 = getCrossCookBoy2(activityId);
            if (crossCookBoy2 != null) {
                crossCookBoy2.addCookBoy2Horse(serverId, userId, dishId, time);
            }
    }

    /*↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓小当家一↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓1*/
    public static CookBoyMatch getCookBoyMatch(int activityId, int no) {
        synchronized (getActivityLock(activityId)) {
            CrossCookBoy crossCookBoy = getCrossCookBoy(activityId);
            if (crossCookBoy != null) {
                return crossCookBoy.getCookBoyMatch(no);
            }
        }
        return null;
    }

    public static Map<Integer, CookBoyMatch> getCookBoyMatch(int activityId) {
        synchronized (getActivityLock(activityId)) {
            CrossCookBoy crossCookBoy = getCrossCookBoy(activityId);
            if (crossCookBoy != null) {
                return crossCookBoy.getCookBoyMatch();
            }
        }
        return null;
    }

    public static Map<Integer, CookBoyMatch> getCookBoyMatchHistory(int activityId) {
        synchronized (getActivityLock(activityId)) {
            CrossCookBoy crossCookBoy = getCrossCookBoy(activityId);
            if (crossCookBoy != null) {
                return crossCookBoy.getCookBoyMatchHistory();
            }
        }
        return null;
    }

    public static CookBoyMatchReward getMatchReward(int activityId, long userId, int no) {
        CookBoyMatchReward cookBoyMatchReward = new CookBoyMatchReward();
        synchronized (getActivityLock(activityId)) {
            CrossCookBoy crossCookBoy = getCrossCookBoy(activityId);
            if (crossCookBoy != null) {
                return crossCookBoy.getMatchReward(userId, no);
            }
        }
        cookBoyMatchReward.setRet(GameErrorCode.E_RELATION_ACTIVITY_NO_IN_TIME);
        return cookBoyMatchReward;
    }

    //触发比赛是否结束
    public static void matchCheck(int activityId) {
        synchronized (getActivityLock(activityId)) {
            CrossCookBoy crossCookBoy = getCrossCookBoy(activityId);
            if (crossCookBoy != null) {
                crossCookBoy.matchCheck();
            }
        }
    }

    public static void serveFood(int activityId, int no, CookBoyMatchDish cookBoyMatchFood, CookBoyProto.CookBoyFinishFoodTempMsg tempFood) {
        synchronized (getActivityLock(activityId)) {
            CrossCookBoy crossCookBoy = getCrossCookBoy(activityId);
            if (crossCookBoy != null) {
                crossCookBoy.serverFoodAll(no, cookBoyMatchFood, tempFood);
            }
        }
    }

    public static void syncFoodTable(int activityId, long serverId, long userId) {
        synchronized (getActivityLock(activityId)) {
            CrossCookBoy crossCookBoy = getCrossCookBoy(activityId);
            if (crossCookBoy != null) {
                crossCookBoy.syncFoodTable(serverId, userId);
            }
        }
    }

    public static void syncAllServerFoodTable(int activityId) {
        synchronized (getActivityLock(activityId)) {
            CrossCookBoy crossCookBoy = getCrossCookBoy(activityId);
            if (crossCookBoy != null) {
                crossCookBoy.syncAllServer(0);
            }
        }
    }

    private static CrossCookBoy getCrossCookBoy(int activityId) {
        if (crossCookBoyMap.containsKey(activityId)) {
            return crossCookBoyMap.get(activityId);
        } else {
            CrossCookBoyActivityMgr.reloadActivityInfo(activityId);
            ActivityInfo acInfo = CrossCookBoyActivityMgr.getOpenActivityInfo(activityId);
            if (acInfo != null) {
                if(acInfo.getType() != eActivityType.CookingBoyActivity.getValue()){
                    return null;
                }
                CrossCookBoy crossCookBoy = new CrossCookBoy(acInfo);
                if (crossCookBoy.init()) {
                    crossCookBoyMap.put(activityId, crossCookBoy);
                } else {
                    logger.error("跨服小当家活动" + activityId + "加载失败");
                }
                return crossCookBoyMap.get(activityId);
            }
        }
        return null;
    }


    /*↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓通用↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓1*/

    public static void timerTask() {
        List<Integer> removeList = new ArrayList<>();
        int currentSecond = DateHelper.getCurrentSecond();
        for (CrossCookBoy crossCookBoy : crossCookBoyMap.values()) {
            synchronized (getActivityLock(crossCookBoy.getActivityId())) {
                crossCookBoy.timerTask();
            }
            if (currentSecond >= crossCookBoy.getActivityInfo().getEndShowTime() + 5 * 60) {
                removeList.add(crossCookBoy.getActivityId());
            }
        }
        for (Integer activityId : removeList) {
            crossCookBoyMap.remove(activityId);
        }
        removeList.clear();


        for (CrossCookBoy2 crossCookBoy2 : crossCookBoy2Map.values()) {
            synchronized (getActivityLock(crossCookBoy2.getActivityId())) {
                crossCookBoy2.timerTask();
            }
            if (currentSecond >= crossCookBoy2.getActivityInfo().getEndTime() + 5 * 60) {
                crossCookBoy2.sendAutoJoinSpoonRest(crossCookBoy2.getActivityInfo().getActivityId()); // 活动过期将设置的自动回访汤勺返回
            }
            if (currentSecond >= crossCookBoy2.getActivityInfo().getEndShowTime() + 5 * 60) {
                removeList.add(crossCookBoy2.getActivityId());
            }
        }
        for (Integer activityId : removeList) {
            crossCookBoy2Map.remove(activityId);
        }
        removeList.clear();

        for (CrossCookBoy3 crossCookBoy3 : crossCookBoy3Map.values()) {
            crossCookBoy3.timerTask();
            if (currentSecond >= crossCookBoy3.getActivityInfo().getEndShowTime() + 5 * 60) {
                removeList.add(crossCookBoy3.getActivityId());
            }
        }

        for (Integer activityId : removeList) {
            crossCookBoy3Map.remove(activityId);
        }
        removeList.clear();
    }

    public static void timerTaskBoy2() {
        for (CrossCookBoy2 crossCookBoy2 : crossCookBoy2Map.values()) {
            if(ActivityMgr.activityInTime(crossCookBoy2.getActivityInfo())){
                if(!crossCookBoy2.getActivityInfo().getServerIdList2().isEmpty()){
                    List<Long> needSyncServerIdList = crossCookBoy2.getNeedSyncServerIdList();
                    if(!needSyncServerIdList.isEmpty()){
                        crossCookBoy2.setSyncServerIdList(new ConcurrentHashSet<>(needSyncServerIdList));
                    }
                }
            }
        }
    }

    public static void syncCookBoy3LoginData(int activityId, long serverId, long userId) {
            CrossCookBoy3 crossCookBoy3 = getCrossCookBoy3(activityId);
            if (crossCookBoy3 != null) {
                crossCookBoy3.syncLoginData(serverId, userId);
            }
    }

    public static void noticeTimerTask() {
        for (CrossCookBoy3 crossCookBoy3 : crossCookBoy3Map.values()) {
                crossCookBoy3.noticeTimerTask();
        }
    }

    private static synchronized Object getActivityLock(int activityId) {
        if (activityLock == null) {
            activityLock = new ConcurrentHashMap<>();
        }
        if (!activityLock.containsKey(activityId)) {
            activityLock.put(activityId, new Object());
        }
        return activityLock.get(activityId);
    }

    public static void loadActivity(int activityId) {
        synchronized (getActivityLock(activityId)) {
            ActivityInfo acInfo = CrossCookBoyActivityMgr.getOpenActivityInfo(activityId);
            if (acInfo != null) {
                if (acInfo.getChildType() == eActivityCookBoyType.Process1.getChildType()) {
                    if (!crossCookBoyMap.containsKey(activityId)) {
                        CrossCookBoy crossCookBoy = new CrossCookBoy(acInfo);
                        if (crossCookBoy.init()) {
                            crossCookBoyMap.put(activityId, crossCookBoy);
                        } else {
                            logger.error("跨服小当家1活动" + activityId + "加载失败");
                        }
                    } else {
                        crossCookBoyMap.get(activityId).setActivityInfo(acInfo);
                    }
                } else if (acInfo.getChildType() == eActivityCookBoyType.Process2.getChildType()) {
                    if (!crossCookBoy2Map.containsKey(activityId)) {
                        CrossCookBoy2 crossCookBoy2 = new CrossCookBoy2(acInfo);
                        if (crossCookBoy2.init()) {
                            crossCookBoy2Map.put(activityId, crossCookBoy2);
                        } else {
                            logger.error("跨服小当家2活动" + activityId + "加载失败");
                        }
                    } else {
                        crossCookBoy2Map.get(activityId).setActivityInfo(acInfo);
                    }
                } else if (acInfo.getChildType() == eActivityCookBoyType.Process3.getChildType()) {
                    if (!crossCookBoy3Map.containsKey(activityId)) {
                        CrossCookBoy3 crossCookBoy3 = new CrossCookBoy3(acInfo);
                        if (crossCookBoy3.init()) {
                            crossCookBoy3Map.put(activityId, crossCookBoy3);
                        } else {
                            logger.error("跨服小当家3活动" + activityId + "加载失败");
                        }
                    } else {
                        crossCookBoy3Map.get(activityId).setActivityInfo(acInfo);
                    }
                }
            }
        }
    }

    public static void removeActivity() {
        Map<Integer, ActivityInfo> activityInfoMap = CrossRelationActivityMgr.getOpenActivityInfoMap(eActivityType.CookingBoyActivity.getValue());
        for (CrossCookBoy crossCookBoy : crossCookBoyMap.values()) {
            if (!activityInfoMap.containsKey(crossCookBoy.getActivityId())) {
                removeData(crossCookBoy.getActivityId());
            }
        }
        for (CrossCookBoy2 crossCookBoy2 : crossCookBoy2Map.values()) {
            if (!activityInfoMap.containsKey(crossCookBoy2.getActivityId())) {
                removeData(crossCookBoy2.getActivityId());
            }
        }
        for (CrossCookBoy3 crossCookBoy3 : crossCookBoy3Map.values()) {
            if (!activityInfoMap.containsKey(crossCookBoy3.getActivityId())) {
                removeData(crossCookBoy3.getActivityId());
            }
        }
    }

    private static void removeData(int activityId) {
        crossCookBoyMap.remove(activityId);
        crossCookBoy2Map.remove(activityId);
        crossCookBoy3Map.remove(activityId);
        activityLock.remove(activityId);
        logger.error("remove cookBoy activity:{}", activityId);
    }

    public static void checkDishStatus(int activityId, long serverId, List<CookBoyProto.NeedCheckCookBoy2DishMsg> dishList) {
        synchronized (getActivityLock(activityId)) {
            CrossCookBoy2 crossCookBoy2 = getCrossCookBoy2(activityId);
            if (crossCookBoy2 != null) {
                crossCookBoy2.checkDishStatus(serverId,dishList);
            }
        }
    }

    public static void syncActivityBack(int activityId, long serverId) {
        CrossCookBoy2 crossCookBoy2 = getCrossCookBoy2(activityId);
        if(crossCookBoy2 != null){
            crossCookBoy2.syncActivityBack(serverId);
        }
    }

    //参加赴宴范围内的，来补充记录
    public static void syncJoinDataFromOtherActivity(int activityId, long homeUserId, long joinUserId, int otherActivityId) {
        CrossCookBoy2 crossCookBoy2 = getCrossCookBoy2(activityId);
        if(crossCookBoy2 != null){
            crossCookBoy2.newJoin(homeUserId, joinUserId, otherActivityId);
        }
    }

    @Override
    public boolean reloadConfig() throws Exception {
        return true;
    }

    @Override
    public boolean reloadData() throws Exception {
        return true;
    }

    public boolean save() {
        for (CrossCookBoy crossCookBoy : crossCookBoyMap.values()) {
            crossCookBoy.save();
        }
        for (CrossCookBoy2 crossCookBoy2 : crossCookBoy2Map.values()) {
            crossCookBoy2.save();
        }
        for (CrossCookBoy3 crossCookBoy3 : crossCookBoy3Map.values()) {
            crossCookBoy3.save();
        }
        return true;
    }

    public boolean init() throws Exception {
        return reload();
    }

    public boolean stop() {
        return false;
    }

    /**
     * 预留接口
     */
    public static void repair(int activityId) {
        CrossCookBoy crossCookBoy = crossCookBoyMap.get(activityId);
        if (crossCookBoy != null) {
            crossCookBoy.repair();
        }
        CrossCookBoy2 crossCookBoy2 = crossCookBoy2Map.get(activityId);
        if (crossCookBoy2 != null) {
            crossCookBoy2.repair();
        }
        CrossCookBoy3 crossCookBoy3 = crossCookBoy3Map.get(activityId);
        if (crossCookBoy3 != null) {
            crossCookBoy3.repair();
        }
    }

    public boolean removeExpireData(){
        List<Integer> cook1List = new ArrayList<>();
        List<Integer> cook2List = new ArrayList<>();
        List<Integer> cook3List = new ArrayList<>();
        long nowTime = System.currentTimeMillis();
        for(Integer activityId : crossCookBoyMap.keySet()){
            CrossCookBoy data = crossCookBoyMap.get(activityId);
            if(data.getActivityInfo() == null || (nowTime > data.getActivityInfo().getEndShowTime()  * 1000 + 3 * 24 * 3600 * 1000)){
                cook1List.add(activityId);
            }
        }

        for(Integer activityId : crossCookBoy2Map.keySet()){
            CrossCookBoy2 data = crossCookBoy2Map.get(activityId);
            if(data.getActivityInfo() == null || (nowTime > data.getActivityInfo().getEndShowTime()  * 1000 + 3 * 24 * 3600 * 1000)){
                cook2List.add(activityId);
            }
        }

        for(Integer activityId : crossCookBoy3Map.keySet()){
            CrossCookBoy3 data = crossCookBoy3Map.get(activityId);
            if(data.getActivityInfo() == null || (nowTime > data.getActivityInfo().getEndShowTime()  * 1000 + 3 * 24 * 3600 * 1000)){
                cook3List.add(activityId);
            }
        }
        for(Integer activityId : cook1List){
            crossCookBoyMap.remove(activityId);
        }
        for(Integer activityId : cook2List){
            crossCookBoy2Map.remove(activityId);
        }
        for(Integer activityId : cook3List){
            crossCookBoy3Map.remove(activityId);
        }

        return true;
    }

    /**
     * 获取自动回访记录
     *
     * @param activityId 活动ID
     * @param serverId   服务器ID
     * @param userId     用户ID
     */
    public static void getAutoJoinRecord(int activityId, long serverId, long userId) {
        CrossCookBoy2 crossCookBoy2 = getCrossCookBoy2(activityId);
        if (crossCookBoy2 != null) {
            crossCookBoy2.getAutoJoinRecord(serverId, userId);
        }
    }

    /**
     * 获取摊主可试吃数量
     *
     * @param serverId 回发游戏服ID
     * @param activityId 活动ID
     * @param reqMsg reqMsg
     */
    public static void getTasteTableCnt(long serverId, int activityId, CookBoyProto.CrossCookBoy2GetTasteTableCntMsg reqMsg) {
        CrossCookBoy2 crossCookBoy2 = getCrossCookBoy2(activityId);
        if (crossCookBoy2 != null) {
            crossCookBoy2.getTasteTableCnt(serverId, reqMsg);
        }
    }

    /**
     * 自动回访试吃
     *
     */
    public static void autoJoinTaste(long serverId, int activityId, CookBoyProto.CrossCookBoy2AutoJoinTasteMsg reqMsg) {
        CrossCookBoy2 crossCookBoy2 = getCrossCookBoy2(activityId);
        if (crossCookBoy2 != null) {
            crossCookBoy2.autoJoinTaste(serverId, reqMsg);
        }
    }

    /**
     * 自动回访
     *
     */
    public static void syncAutoJoinData(long serverId, int activityId, CookBoyProto.CrossCookBoy2SyncAutoJoinDataMsg reqMsg) {
        CrossCookBoy2 crossCookBoy2 = getCrossCookBoy2(activityId);
        if (crossCookBoy2 != null) {
            crossCookBoy2.syncAutoJoinData(serverId, reqMsg);
        }
    }

    /**
     * 自动回访
     *
     */
    public static void syncAutoJoinSpoon(long serverId, int activityId, CookBoyProto.CrossCookBoy2SyncAutoJoinSpoonMsg reqMsg) {
        CrossCookBoy2 crossCookBoy2 = getCrossCookBoy2(activityId);
        if (crossCookBoy2 != null) {
            crossCookBoy2.syncAutoJoinSpoon(serverId, reqMsg);
        }
    }
}
